package org.agilar.gwt.sortabletable;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class PageIndex extends Composite implements TableModelListener, ClickListener
{
	private HorizontalPanel panel = new HorizontalPanel();
	private PagedTableModel model;
	private Label previous;
	private Label next;
	private Map<Label, Integer> pageLinks = new HashMap<Label, Integer>();
	
	public PageIndex()
	{
		initWidget(panel);
		addStyleName("agilar-sortabletable-pageIndex");
		previous = new Label("<<"); 
		next = new Label(">>");
		previous.addStyleName("agilar-sortabletable-pageLink");
		previous.addClickListener(this);
		next.addClickListener(this);
		next.addStyleName("agilar-sortabletable-pageLink");
	}


	public void modelChanged()
	{
		update();
	}


	private void update()
	{
		panel.clear();
		if (model.getPageCount() > 1)
		{
			if (model.getPage() > 0)
			{
				panel.add(previous);
			}
			pageLinks.clear();
			int start = model.getPage() - 3;
			int end = model.getPage() + 3;
			if (start < 0)
			{
				start = 0;
			}
			if (end > model.getPageCount())
			{
				end = model.getPageCount();
			}
			
			if (start > 0)
			{
				Label l = new Label("1");
				l.addClickListener(this);
				l.addStyleName("agilar-sortabletable-pageLink");
				panel.add(l);
				pageLinks.put(l, 0);
				if (start > 1)
				{
					l = new Label("...");
					l.addStyleName("agilar-sortabletable-ellipsis");
					panel.add(l);
				}
			}
			
			for (int i=start; i<end; i++)
			{
				Label l = new Label(""+(i+1));
				if (i == model.getPage())
				{
					l.addStyleName("agilar-sortabletable-pageLink-selected");
				}
				else
				{
					l.addClickListener(this);
					l.addStyleName("agilar-sortabletable-pageLink");
				}
				pageLinks.put(l, i);
				panel.add(l);
			}
	
			if (end < model.getPageCount())
			{
				if (end < model.getPageCount()-1)
				{
					Label l = new Label("...");
					l.addStyleName("agilar-sortabletable-ellipsis");
					panel.add(l);
				}
				Label l = new Label(""+model.getPageCount());
				l.addClickListener(this);
				l.addStyleName("agilar-sortabletable-pageLink");
				panel.add(l);
				pageLinks.put(l, model.getPageCount()-1);
			}
	
			if (model.getPage() < model.getPageCount()-1)
			{
				panel.add(next);
			}
		}
	}


	public PagedTableModel getModel()
	{
		return model;
	}


	public void setModel(PagedTableModel model)
	{
		if (this.model != null)
		{
			this.model.removeTableModelListener(this);
		}
		this.model = model;
		this.model.addTableModelListener(this);
		update();
	}

	public void onClick(Widget sender)
	{
		if (sender == previous)
		{
			model.setPage(model.getPage()-1);
		}
		else if (sender == next)
		{
			model.setPage(model.getPage()+1);
		}
		else
		{
			if (pageLinks.containsKey(sender))
			{
				model.setPage(pageLinks.get(sender));
			}
		}
	}
}
